<!--  -->
<template>
  <div style="padding-top:100px">
    <div class="app-title-container title-position">
      <modules-title titleName="送检申请详情">
        <template slot="btn" v-if="form">
          <el-button class="filter-item" plain="" size="small" @click="handleBack">返回
          </el-button>
          <el-button type="primary" size="small"
                     v-if="form.inspection_status ===5"
                     @click="handlReport(form.id)">查看报告
          </el-button>

          <el-button type="primary" size="small"
                     v-if="form.inspection_status === 2 && form.if_undo === 0 && form.creator_id === userInfo"
                     @click="handlUndo(form.id)">撤销检验
          </el-button>
          <el-button type="primary" size="small" @click="handlUndoId('ApprovalCheck',form.apply_check.id)"
                     v-if="form.apply_check.Cancelable && boole">
            撤销
          </el-button>
          <el-button type="primary" size="small" @click="handlUndoId('RepealCheck',form.undo_check.id)"
                     v-if="form.undo_check && form.undo_check.Cancelable && boole1">
            撤销
          </el-button>
          <el-button type="primary" size="small" @click="handlEdit(form.id)"
                     v-if="boole&& form.inspection_status === 6">
            编辑
          </el-button>
        </template>
      </modules-title>
    </div>

    <div style="padding:20px">
      <ruleForm :form="form"/>
      <log ref="log"/>
    </div>

    <!--查看报告 -->
    <reportdialog  ref="reportdialog"  :formOpen="formOpen" :rowList="rowList"/>
  </div>
</template>

<script>
  import {
    detail,
    viewQuality,
    flowDetail,
    applyViewReport,
    undo,
    applyUndo, sampleListReport, sampleListTable
  } from '@/api/sampleInspection'

  export default {
    components: {
      log: () => import('./modules/log'),
      ruleForm: () => import('../modules/ruleForm'),
      reportdialog: () => import('../modules/reportList.vue')
    },
    data() {
      return {
        title: '详情',
        left: 'left',
        form: null,
        openReport: false,
        fileType: undefined,
        fileList: [],
        userInfo: null,
        boole: false,
        boole1: false,
        rowList:{},
        formOpen:{
          tableResults: {
            id: '',
            report: [],//
            computation_sheet: [], //计算表格
            inspection_map: []//  检验图谱
          },
          relation_plan_id: ''
        }
      }
    },
    //是计算属性，也可以理解为一个方法。
    computed: {},
    //属性是手动定义的所需监听的值
    watch: {},
    //生命周期 - 创建完成（访问当前this实例）
    created() {
      this.userInfo = this.$store.getters.userInfo.user.id
      this.detailData()
    },
    //生命周期 - 挂载完成（访问DOM元素）
    mounted() {
    },
    //是Vue实例对象上绑定的方法，供当前Vue组件作用域内使用，
    methods: {
      handlEdit(id) {
        this.$router.push({
          path: '/sampleInspection/applied/createTest',
          query: { s_id: id }
        })
      },
      //获取详情
      detailData() {
        let commit = undefined,
          id = ''
        if (this.$route.query.id) {
          commit = detail
          id = this.$route.query.id
        }
        if (this.$route.query.f_id) {
          commit = flowDetail
          id = this.$route.query.f_id
        }
        if (id !== '') {
          commit(id).then(res => {
            if (res.code === 200) {
              this.form = res.data || []
              if (this.form.apply_check.check_creator_id === this.userInfo) {
                this.boole = true
              }
              if (this.form.undo_check && this.form.undo_check.check_creator_id === this.userInfo) {
                this.boole1 = true
              }
              if (
                this.form.quality_standard_file &&
                this.form.quality_standard_file.length > 0 &&
                this.form.quality_standard_id
              ) {
                viewQuality(this.form.quality_standard_id).then(res => {
                  if (res.code === 200) {
                    this.form.quality_standard_file = res.data.file || []
                  }
                })
              }
            }
          })
        }
      },

      //返回
      handleBack() {
        this.$route.params.refresh = true
        this.$router.go(-1)
      },

      //查看报告
      handlReport(id) {
        let row = {}
        sampleListReport(id).then(res=>{
          if(res.code === 200){
            row = res.data
            this.rowList = row
            this.formOpen.tableResults = {
              computation_sheet: [],
              report: [],
              inspection_map: []
            }
            if (row.computation_sheet && row.inspection_result && row.inspection_map) {
              this.formOpen.tableResults.computation_sheet = JSON.parse(JSON.stringify(row.computation_sheet)) || []
              this.formOpen.tableResults.report = JSON.parse(JSON.stringify(row.inspection_result)) || []
              this.formOpen.tableResults.inspection_map = row.inspection_map || []
              for (let i = 0; i < this.formOpen.tableResults.report.length; i++) {
                this.formOpen.tableResults.report[i].boole = true
                if (row.experiment_table_info_map) {
                  this.formOpen.tableResults.report[i].ruleForm = row.experiment_table_info_map[this.formOpen.tableResults.report[i].batch_no]

                } else {
                  if (row.inspection_type === 'inspection_type_wd') {
                    sampleListTable({ id: row.relation_plan_id }).then(res => {
                      if (res.code === 200) {
                        this.formOpen.tableResults.report.forEach(item => {
                          if (res.data[item.batch_no]) {
                            item.ruleForm = res.data[item.batch_no]

                          }
                        })
                      }
                    })
                  }
                }
              }
            }
            this.$refs.reportdialog.openport()
            this.activeNames = this.formOpen.tableResults.report[0].batch_no
          }
        })

      },

      handlUndoId(val, id) {

        let data = {
          flow_id: id,
          model_type: val
        }
        applyUndo(data).then(res => {
          if (res.code == 200) {
            this.$message.success('操作成功')
            this.$refs.log.historyData()
            this.detailData()
          }
        })
      },

      //撤销审批
      handlUndo(id) {
        console.log(id)
        this.$confirm('确认要撤销检验', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(_ => {
            undo(id).then(res => {
              if (res.code === 200) {
                this.$message.success('操作成功')
                this.$refs.log.historyData()
                this.detailData()
              } else {
                this.$message.error(res.msg)
              }
            })
          })
          .catch(_ => {
          })
      }
    }
  }
</script>
<style scoped>
  /* @import url(); 引入css类 */
  .el-row {
    display: flex;
    justify-content: flex-start;
    flex-flow: wrap;
  }
</style>
